import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;


public class Order {

	private Invoice invoice;
	private List<Invoice> invoices = new ArrayList<Invoice>();
	public static List<Invoice> invoices_global = new ArrayList<Invoice>();
	private Klient klient;
	
	//do wyszukiwania klient�w kt�rzy z�o�yli kiedykolwiek zam�wienie
	public static TreeMap< String,  Klient> klient_order = new TreeMap<String, Klient>();
	
	
	
	Order(Klient klient){
		this.klient = klient;
		dodajKwalifokowane(klient);
	}
	
	private void dodajKwalifokowane(Klient klient) {
		if(!klient_order.containsKey(klient.getId())){
			klient_order.put(klient.getId(), klient);
		}
	}

	public static Klient znajdzKlientaPoId(String id) throws Exception{
		if(!klient_order.containsKey(id)){
			throw new Exception("Nie odnaleziono klienta o id: "+id);
		}		
		return klient_order.get(id);
		
	}
	
	
	public Klient getClient(){
		return klient;
		
	}

	public Invoice getInvoice(int i) {
		return invoices.get(i);
	}

	public void setInvoice(Invoice invoice) {
		if(!invoices.contains(invoice)){
			
			if(!invoices_global.contains(invoice)){
				invoices.add(invoice);
				invoices_global.add(invoice);
			}
			
			else {
				System.out.println("zakac wsp�dzielenia faktur");
			}
		}
		
		else {
			System.out.println("zakac wsp�dzielenia faktur");
		}
		
		}
	
	@Override
	public String toString(){
	String f = "";
		for(Invoice i: invoices){
			f = f+ i.toString()+"\n";
		}
		
		return "Zam�wienie klienta: "+klient+" dotyczy faktur: "+f;
	}
	
	
	public static void main(String[] args) throws Exception {
		Klient k = new Klient("Dziobkowski", "AD");
		Klient k2 = new Klient("Frankowski", "FG");
		Order o = new Order(k);
		
		Invoice.getInvoice(o);
		System.out.println(o);
		
		Order o2 = new Order(k2);
		
		o2.setInvoice(o.getInvoice(0));
		System.out.println(o2);
		
		Invoice.getInvoice(o2);
		System.out.println(o2);
		
//		Klient k = new Klient("Dziobkowski", "AD");
//		Klient k2 = new Klient("Frankowski", "FG");
//		
//		Order o = new Order(k);
//		Invoice.getInvoice(o);
//		
//		Klient k3 = o.znajdzKlientaPoId("AD");
//		System.out.println(k3);
//		
		
	}
	
}
